package com.beiben.work.process.controller;

import com.beiben.common.GlobalConstant;
import com.beiben.system.file.domain.File;
import com.beiben.system.file.service.IFileService;
import com.beiben.work.process.domain.FileProcessFinishedVm;
import com.beiben.work.process.domain.FileProcessUser;
import com.beiben.work.process.service.IFileProcessService;
import com.beiben.work.process.service.IFileProcessUserService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * 任务分配Controller
 *
 * @author xgq
 * @date 2020-08-26
 */
@RestController
@RequestMapping("/work/process/user")
public class FileProcessUserController extends BaseController {

    @Autowired
    private IFileProcessUserService fileProcessUserService;

    @Autowired
    private IFileService fileService;

    @Autowired
    private IFileProcessService fileProcessService;


    /**
     * 查询任务分配列表
     */
    @GetMapping("/list")
    public TableDataInfo list(FileProcessUser fileProcessUser) throws ParseException {
        startPage();
        List<FileProcessUser> list = fileProcessUserService.selectFileProcessUserList(fileProcessUser);
        return getDataTable(list);
    }

    /**
     * 导出任务分配列表
     */
    @Log(title = "任务分配", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(FileProcessUser fileProcessUser) throws ParseException {
        List<FileProcessUser> list = fileProcessUserService.selectFileProcessUserList(fileProcessUser);
        ExcelUtil<FileProcessUser> util = new ExcelUtil<FileProcessUser>(FileProcessUser.class);
        return util.exportExcel(list, "process");
    }

    /**
     * 获取任务分配详细信息
     */
    @GetMapping(value = "/{itemId}")
    public AjaxResult getInfo(@PathVariable("itemId") Long itemId) {
        AjaxResult ajax = AjaxResult.success();
        FileProcessUser fileProcessUser = fileProcessUserService.selectFileProcessUserById(itemId);
        if(null!=itemId){
            ajax.put("users", fileProcessService.getUserInfoEdit(itemId,fileProcessUser.getWorkId()));
            ajax.put("userId", fileProcessUser.getUserId());
            ajax.put("groupIds", fileProcessUserService.selectFileProcessGroupByItemId(itemId));
        }
        ajax.put(AjaxResult.DATA_TAG,fileProcessUser);
        return ajax;
    }

    /**
     * 新增任务分配
     */
    @Log(title = "任务分配", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FileProcessUser fileProcessUser) {
        return toAjax(fileProcessUserService.insertFileProcessUser(fileProcessUser));
    }

    /**
     * 修改任务分配
     */
    @Log(title = "任务分配", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FileProcessUser fileProcessUser) {
        return toAjax(fileProcessUserService.updateFileProcessUser(fileProcessUser));
    }

    /**
     * 删除任务分配
     */
    @Log(title = "任务分配", businessType = BusinessType.DELETE)
    @DeleteMapping("/{itemIds}")
    public AjaxResult remove(@PathVariable Long[] itemIds) {
        return toAjax(fileProcessUserService.deleteFileProcessUserByIds(itemIds));
    }

    /**
     * 上传见证性材料
     *
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/upload")
    @ResponseBody
    public AjaxResult uploadFile(@RequestParam("file") MultipartFile[] file, Long tableId) {
        try {
            fileService.uploadFile(file, tableId, GlobalConstant.WORK_FILE_PROCESS_USER,GlobalConstant.WORK_FILE_SERVER_ADDRESS);
            AjaxResult ajax = AjaxResult.success();
            return ajax;
        } catch (Exception e) {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 完成任务分配
     */
    @Log(title = "完成任务分配", businessType = BusinessType.UPDATE)
    @PostMapping("/finishTime")
    public AjaxResult finished(@RequestBody FileProcessFinishedVm vm) {
        return toAjax(fileProcessUserService.finishedFileProcessUserByIds(vm.getItemIds(), vm.getRealFinishTime(), vm.getWorkId()));
    }

    /**
     * 任务状态修改
     */
    @PostMapping("/checkState")
    public AjaxResult checkState(@RequestBody FileProcessFinishedVm vm) {
        return toAjax(fileProcessUserService.changeStatesByIds(vm.getItemIds(),vm.getState()));
    }

    /**
     * 按itemId获取文件列表
     *
     * @return
     */
    @Log(title = "按ItemId获取文件列表", businessType = BusinessType.UPDATE)
    @PostMapping("/files/{itemId}")
    public TableDataInfo selectFiles(@PathVariable Long itemId) {
        startPage();
        List<File> list = fileProcessUserService.selectFiles(itemId);
        return getDataTable(list);
    }

    /**
     * 按itemId获取文件列表及相关文件列表
     *
     * @return
     */
    @Log(title = "按ItemId获取文件列表", businessType = BusinessType.UPDATE)
    @GetMapping("/allFiles/{itemId}")
    public AjaxResult selectAllFiles(@PathVariable Long itemId) {
        startPage();
        Map<String,List<File>> allFiles = fileProcessUserService.selectAllFiles(itemId);
        return AjaxResult.success(allFiles);
    }


}
